package com.lagou.client;


import com.lagou.model.User;
import com.lagou.model.UserMessage;

import java.io.IOException;

public class ClientView {
    //为了可以使用输入输出流，采用合成复用原则
    private ClientInitClose cic = null;
    private ManagerView mv = null;

    //通过构造方法实现成员变量的初始化
    public ClientView(ClientInitClose cic, ManagerView mv){
        this.cic = cic;
        this.mv = mv;
    }
    //显示方法
    public void clientShow() throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println("欢迎进入在线考试系统");
            System.out.println("-----------------");
            System.out.println("[1] 学员系统  \t");
            System.out.println("[2] 管理员系统 \t");
            System.out.println("[0] 退出考试系统 \t");


            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    clientStudentLogin();
                    break;
                case 2:
                    clientManagerLogin();
                    break;
                case 3:
                    System.out.println("退出系统");
                    return;
                default:
                    System.out.println("输入错误");
            }
        }
    }

    private void clientManagerLogin() throws IOException, ClassNotFoundException {
        String clientMessage = "manager";
        cic.getOos().writeObject(clientMessage);
        //cic.getOis().readObject();
        managerChecker();
    }

    private void clientStudentLogin() throws IOException {
        String clientMessage = "student";
        cic.getOos().writeObject(clientMessage);
        studentChecker();


    }
    //检查学员用户名和密码
    private void studentChecker() throws IOException{
        System.out.println("请输入学员的名称:");
        String stuName = ClientScanner.getScanner().nextLine();
        System.out.println("请输入密码: ");
        String stuPassWord = ClientScanner.getScanner().next();
        UserMessage stuMess = new UserMessage("studentCheck", new User(stuName,stuPassWord));
        cic.getOos().writeObject(stuMess);

    }
    //检查管理员用户名和密码
    private void managerChecker() throws IOException, ClassNotFoundException {
        //打包管理员的数据
        System.out.println("请输入管理员名称：");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入管理员密码");
        String passWord = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("managerCheck", new User(userName,passWord));
        //写入到输出流中
        cic.getOos().writeObject(tum);
        //读取服务器返回的信息
        UserMessage um = (UserMessage) cic.getOis().readObject();
        //检查服务器返回的验证信息
        if(um.getType().equals("success")){//成功进入界面
            mv.managerShow();
        }else{//失败了 退出去
            System.out.println("账户密码错误");
        }
    }
}
